package com.dao;

import java.io.*;
import java.sql.*;
import java.sql.Date;
import java.util.*;

import com.bean.*;

//import java.io.*;

/**
 * @author hxd
 */
public class BlogSQL extends AbstractSQL{
	


	public  ArrayList<Blog> findByKey(String key) {//通过关键词查找
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		//模糊查找
		String sql = "SELECT id,title,content,day,uid FROM blog WHERE title like ? or content like ?";
		ArrayList<Blog> blogs=new ArrayList<Blog>();
		try {
			conn=getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "%"+key+"%");
			pstmt.setString(2, "%"+key+"%");
			rs = pstmt.executeQuery();
			while(rs.next()) {
				//String content=rs.getString(3);
				String head=rs.getString(2);
				int id=rs.getInt(1);
				Timestamp time=rs.getTimestamp(4);
				int uid=rs.getInt(5);
				blogs.add(new Blog(id,uid,head,time));
			}
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return blogs;
	}
	
	public  ArrayList<Blog> findTitle(int uid) { //按作者id查找,方便列出用户的所有博客
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT id,title,day FROM blog WHERE uid=?";// 不找博客的正文,节省内存
		ArrayList<Blog> blogs=new ArrayList<Blog>();
		try {
			conn=getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, uid);
			rs = pstmt.executeQuery();
			while(rs.next()) {
				String head=rs.getString(2);
				int id=rs.getInt(1);
				Timestamp time=rs.getTimestamp(3);
				blogs.add(new Blog(id,uid,head,time));
			}
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return blogs;
	}
	public  Blog find(int id) { //精确查找博客博客
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "SELECT  title,content,day,uid FROM blog WHERE id=?";// 
		Blog blog=null;
		try {
			conn=getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			
			rs = pstmt.executeQuery();
			while(rs.next()) {
				String content=rs.getString(2);
				String head=rs.getString(1);
				Timestamp time=rs.getTimestamp(3);
				int uid=rs.getInt(4);
				blog=new Blog(id,head,content,uid,time);
			}
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return blog;
	}
	

	public  boolean addBlog(int uid,Blog blog){
		String sql="insert into blog(uid,title,content,day)values (?,?,?,now())";
		Connection conn = null;
		PreparedStatement pstmt = null;

			try {
				conn=getConnection();
				pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, uid);
				pstmt.setString(2, blog.getTitle());
			pstmt.setString(3, blog.getContent());
			pstmt.executeUpdate();
		
			} catch (SQLException  e) {
				return false;
			}
			try {
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return true;
		
	}
	
	
	public  void update(Blog blog){
		String sql="update blog set title=?,content=? where id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn=getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(3, blog.getId());
			pstmt.setString(1, blog.getTitle());
			pstmt.setString(2, blog.getContent());
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
			
		} catch (SQLException  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public  void delete(int id){
		String sql="delete from blog where id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			conn=getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, id);
			
			pstmt.executeUpdate();
			pstmt.close();
			conn.close();
			
		} catch (SQLException  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		List<Blog>blist=new BlogSQL().findTitle(1);
		for(Blog b:blist)
			System.out.println(b.getTime().toString().replace(".0", ""));
	}	
	}

